package com.zsh;

import com.zsh.mapper.AccountMapper;
import com.zsh.model.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * TODO
 *
 * @author zsh
 * @date 2020-12-07 20:18
 */
public class MyBatisTest {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void before() throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
        inputStream.close();
    }

    @Test
    public void testOne2One() {
        SqlSession session = null;
        try {
            Thread.currentThread().setName("nihao");
            session = sqlSessionFactory.openSession();
            AccountMapper accountMapper = session.getMapper(AccountMapper.class);
            Account account = accountMapper.selectByPrimaryKey(1);
            System.out.println(account);
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    @Test
    public void tesGetAccount() {
        SqlSession session = null;
        try {
            session = sqlSessionFactory.openSession();
            AccountMapper accountMapper = session.getMapper(AccountMapper.class);
            List<Account> list = accountMapper.getAccount(1, "111");
            System.out.println(list);
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }


    @Test
    public void test$sql() throws Exception {
        SqlSession session = null;
        try {
            session = sqlSessionFactory.openSession();
            AccountMapper accountMapper = session.getMapper(AccountMapper.class);
            Account account = accountMapper.getAccountById(1);
            System.out.println(account);
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }


    @Test
    public void testSql() throws Exception {
    }


    @Test
    public void testExecutor() throws Exception {
        // ExecutorService es = Executors.newFixedThreadPool(2);

    }

    /**
     * 类型擦除
     *
     * @throws Exception
     */
    @Test
    public void testTypeErasure() throws Exception {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        Method add = list.getClass().getDeclaredMethod("add", Object.class);
        add.invoke(list,"zsh");
        System.out.println(Arrays.toString(list.toArray()));
    }


}
